package edu.bw.web.user;

import cn.hutool.json.JSONUtil;
import edu.bw.common.CommonResult;
import edu.bw.dto.UpdateUserForm;
import edu.bw.service.UserService;
import edu.bw.service.impl.UserServiceImpl;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

@WebServlet("/user/update")
public class UpdateServlet extends HttpServlet {
    UserService userService=new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //{
        //  "userId": 1,
        //  "username": "123456",
        //  "password": "123456",
        //  "name": "陈辰鲨",
        //  "sex": "男",
        //  "tel": "17858925679",
        //  "email": "3201631766@qq.com",
        //  "hiredate": "2025-05-16",
        //  "role": [
        //    5
        //  ],
        //  "deptId": 6,
        //  "status": 1
        //}
        String reqJSON = request.getReader().readLine();
        UpdateUserForm updateUserForm = JSONUtil.toBean(reqJSON, UpdateUserForm.class);

        Integer rows=userService.updateUser(updateUserForm);

        response.getWriter().write(JSONUtil.toJsonStr(CommonResult.ok().put("rows",rows)));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
